package com.view;

import com.type.Friend;
import com.type.photoByteArray;

import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

public class AppView extends View implements OnGestureListener {
	float xAxis[] = new float[6];
	float yAxis[] = new float[6];
	static float SL = 100;	// Side Length of the square grid
	float TBH = 15;   // Title Bar Height	
	static int NAME_TITLE_COLOR = 0x90 << 24 | 0x00ff0000;
	static int PHONENUMBER_TITLE_COLOR = 0xff << 24 | 0x00ff0000;
	int TEXT_COLOR = Color.argb(0xff, 0xff, 0xff, 0xff);
	float TEXT_BASELINE_RATIO = 0.8f;
	
	GestureDetector mGestureDetector;
	Activity activity;
	
	OperateFriendLayer operateFriendLayer;
	Boolean isOperateFriendLayerOn = false;
	ArrayList<Rect> longPressRespondRectList = new ArrayList<Rect>();
	
	long myPhoneNum;
	ArrayList<Long> phoneNumList;
	ArrayList<String> nameList;
	ArrayList<photoByteArray> avatarList;
	
	
	public AppView(Activity activity, long myPhoneNum, ArrayList<Long> phoneNumList, ArrayList<String> nameList, ArrayList<photoByteArray> avatarList) {
		super(activity);
		this.activity = activity;
		this.myPhoneNum = myPhoneNum;
		this.phoneNumList = phoneNumList;
		this.nameList = nameList;
		this.avatarList = avatarList;
		mGestureDetector = new GestureDetector(this);
		prepareGridLayoutAxisArray(activity.getResources().getConfiguration().orientation);
		setupLongPressRespondArea();
	}
	

	public Activity getActivity() {
		return activity;
	}


	public long getMyPhoneNum() {
		return myPhoneNum;
	}


	public void setIsOperateFriendLayerOn(Boolean isOperateFriendLayerOn) {
		this.isOperateFriendLayerOn = isOperateFriendLayerOn;
	}


	@Override
	public void onDraw(Canvas canvas) {
		int size = phoneNumList.size();
		// TODO zyc future will support more than 6 friends
		if (size > 5)
			return;

		for (int index = 0; index < size; index++) {
			float x = xAxis[index];
			float y = yAxis[index];

			Paint paint = new Paint();

			paint.setColor(NAME_TITLE_COLOR);
			canvas.drawRect(x, y, x + SL, y + TBH, paint);
			paint.setColor(PHONENUMBER_TITLE_COLOR);
			canvas.drawRect(x, y + TBH, x + SL, y + TBH * 2, paint);

			paint.setColor(TEXT_COLOR);
			canvas.drawText(nameList.get(index), x + (SL - paint.measureText(nameList.get(index))) / 2, y + TBH * TEXT_BASELINE_RATIO, paint);
			canvas.drawText(Long.toString(phoneNumList.get(index)), x + (SL - paint.measureText(Long.toString(phoneNumList.get(index)))) / 2, y + TBH * (TEXT_BASELINE_RATIO + 1), paint);

			Bitmap friendPhoto = avatarList.get(index).toBitmap();
			Matrix matrix = new Matrix();
			matrix.postScale(SL / (float) friendPhoto.getWidth(), SL / (float) friendPhoto.getHeight());
			Bitmap scaledFriendPhoto = Bitmap.createBitmap(friendPhoto, 0, 0, friendPhoto.getWidth(), friendPhoto.getHeight(), matrix, true);
			canvas.drawBitmap(scaledFriendPhoto, x, y + TBH * 2, null);
		}
		
		if (isOperateFriendLayerOn)
			drawOperateFriendLayer(canvas);
	}


	@Override
	public void onLongPress(MotionEvent e) {
		if (!isOperateFriendLayerOn) {
			int index = getSelectFriendIndex(e);
			if (index >= 0) {
				String friendPhoneNum = phoneNumList.get(index).toString();
				String name = nameList.get(index);
				Bitmap avatar = avatarList.get(index).toBitmap();
				operateFriendLayer = new OperateFriendLayer(this, friendPhoneNum, name, avatar, (int)xAxis[index], (int)yAxis[index]);
				operateFriendLayer.animationEffect();
				isOperateFriendLayerOn = true;
			}
		}
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	
	public boolean onDown(MotionEvent e) {
		return true;
	}

	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		return true;
	}

	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		return false;
	}

	public void onShowPress(MotionEvent e) {
	
	}

	public boolean onSingleTapUp(MotionEvent e) {
		if (isOperateFriendLayerOn) 
			if (operateFriendLayer != null)
				operateFriendLayer.handleSingleTapUp(e);
		return true;
	}


	public void prepareGridLayoutAxisArray(int orientationType) {
		int columnGridCount = 2, rowGridCount = 3;
		
		if(orientationType==Configuration.ORIENTATION_PORTRAIT) {
			columnGridCount = 2;
			rowGridCount = 3;
		} else if(orientationType==Configuration.ORIENTATION_LANDSCAPE) {
			columnGridCount = 3;
			rowGridCount = 2;
		}
		
		int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
		int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
		float horizontalGap = (screenWidth - columnGridCount * SL) / (columnGridCount + 1);
		float verticalGap = (screenHeight - rowGridCount * (SL + 2 * TBH)) / (rowGridCount + 1);
		
		for (int i=0; i<6; i++) {
			xAxis[i] = horizontalGap + (SL+horizontalGap)*(i%columnGridCount);
			yAxis[i] = verticalGap + (SL+2*TBH+verticalGap) * ((int)(i/columnGridCount));
		}
	}


	void setupLongPressRespondArea() {
		longPressRespondRectList.clear();
		for(int i=0; i<phoneNumList.size(); i++)
			longPressRespondRectList.add( new Rect((int) xAxis[i], (int) yAxis[i], (int) (xAxis[i] + SL), (int) (yAxis[i] + SL + 2 * TBH)) );
	}


	int getSelectFriendIndex(MotionEvent e) {
		for (int i = 0; i < longPressRespondRectList.size(); i++)
			if (longPressRespondRectList.get(i).contains((int)(e.getX()), (int)(e.getY())))
				return i;
		return -1;
	}


	public void drawFriendGrid(Canvas canvas, Friend friend, int index, ArrayList<Long> phoneNumList) {
		// TODO zyc further must change logic
		if(phoneNumList.size()>5)
			return;
		
		float x = xAxis[index];
		float y = yAxis[index];
		
		Paint paint = new Paint();
		
		paint.setColor(NAME_TITLE_COLOR);
		canvas.drawRect(x, y, x + SL, y + TBH, paint);
		paint.setColor(PHONENUMBER_TITLE_COLOR);
		canvas.drawRect(x, y + TBH, x + SL, y + TBH * 2, paint);
		
		paint.setColor(TEXT_COLOR);
		canvas.drawText(friend.getName(), x + ( SL - paint.measureText(friend.getName()) ) / 2, y + TBH*TEXT_BASELINE_RATIO, paint);
		canvas.drawText(Long.toString(friend.getPhoneNumber()), x + ( SL - paint.measureText(Long.toString(friend.getPhoneNumber())) ) / 2, y + TBH * (TEXT_BASELINE_RATIO + 1), paint);
		
		Bitmap friendPhoto = friend.fetchBitmap();
		Matrix matrix = new Matrix();
		matrix.postScale(SL / (float) friendPhoto.getWidth(), SL / (float) friendPhoto.getHeight());
	
		canvas.drawBitmap(Bitmap.createBitmap(friendPhoto, 0, 0, friendPhoto.getWidth(), friendPhoto.getHeight(), matrix, true), x, y + TBH * 2, null);
	}


	private void drawOperateFriendLayer(Canvas canvas) {
		Paint paint = new Paint();
		paint.setAntiAlias(true);
		
		paint.setColor(Color.argb(170, 0, 0, 0));
		canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

		paint.setColor(Color.RED);
		operateFriendLayer.drawImage(canvas, paint);
	}
	
	
	public int getX(int n) {
		return (int)xAxis[n];
	}
	
	public int getY(int n) {
		return (int)yAxis[n];
	}


	public void clearOperateFriendLayer() {
		isOperateFriendLayerOn = false;
		System.gc();
	}
}